<?php

$errors = array();

if ($_POST) {
	// run the validation script
	require_once('library.php');
	try {
		
		// Validate itemName name not empty
		$val = new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::STRING);
		if(!$val->isValid($_POST['itemName'])){
			$errors['itemName']='Item name is required';
		}
		// Validate position not empty and integer
		$val = new Zend_Validate;
		$val->addValidator(new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::STRING));
		$val->addValidator(new Zend_Validate_Int());
		if(!$val->isValid($_POST['position'])){
			$errors['position']='Position requires Integer';
		}
		
		if(!$errors){
			$data = array('accountId' => $_SESSION['accountId'],
						  'position' => $_POST['position'],
			              'itemName' => $_POST['itemName'],
						  'containerId' => $_POST['parentId'],
						  'checkedOut' => 0,
						  'keywords' => $_POST['keywords'],
			              'description' => $_POST['description']);
			$dbWrite->insert('items', $data);
			if($_POST['parentId']==0){
				header('Location: DBMain.php?dbId='.$_POST['dbId'].'&name='.$_GET['name']);
			}else{
				header('Location: DBMain.php?id='.$_POST['parentId'].'&dbId='.$_POST['dbId'].'&name='.$_GET['name']);
			}		  
		}
		
	} catch (Exception $e){
		echo $e->getMessage();
	} 
	
	
}
